package com.kaka.bus.engine.home.info.coupon.page;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.kaka.bus.R;
import com.kaka.bus.adapter.CouponAdapter;
import com.kaka.bus.base.fragment.BaseFragment;
import com.kaka.bus.bean.CouponBean;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.util.U;
import com.kaka.bus.view.MutipleLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangqi on 2016/12/2.
 */

public class UnUseCouponFragment extends BaseFragment<UnUseCouponPresenter> implements UnUseCouponContract.View {


    @BindView(R.id.unusecoupon_recy)
    RecyclerView mUnusecouponRecy;
    @BindView(R.id.unusecoupon_smart)
    SmartRefreshLayout mUnusecouponSmart;
    @BindView(R.id.unusecoupon_muti)
    MutipleLayout mUnusecouponMuti;

    /**
     * 需要激活的couponId
     */
    private String mCouponId;
    private int mPosition;

    public static final String TYPE = "type";
    public static final String UNUSE = "1";
    public static final String LOSE = "2";

    private int mPage = 1;
    private int mPageSize = 20;
    private List<CouponBean> mDatas = new ArrayList<>();
    private String mType;
    private CouponAdapter mCouponAdapter;

    public static UnUseCouponFragment getInstance(String type) {
        UnUseCouponFragment unUseFragment = new UnUseCouponFragment();
        Bundle bundle = new Bundle();
        bundle.putString(TYPE, type);
        unUseFragment.setArguments(bundle);
        return unUseFragment;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);

        Bundle arguments = getArguments();
        mType = arguments.getString(TYPE);

        mUnusecouponRecy.setLayoutManager(new LinearLayoutManager(mActivity));
        mCouponAdapter = new CouponAdapter(R.layout.item_coupon, mDatas);
        mUnusecouponRecy.setAdapter(mCouponAdapter);
        mCouponAdapter.bindToRecyclerView(mUnusecouponRecy);
        mUnusecouponMuti.setEmptyText("您当前暂无消息通知!");
        mUnusecouponMuti.setStatus(MutipleLayout.Loading);
        mUnusecouponMuti.setEmptyBgColor(mActivity.getResources().getColor(R.color.gray_bg));
        mUnusecouponMuti.setErrorBgColor(mActivity.getResources().getColor(R.color.gray_bg));
    }

    @Override
    protected void initData() {
        super.initData();
        getData();
    }

    public void getData() {
        mDatas.clear();
        mPresenter.initData(getUserId(), getToken(), 1, mPageSize,mType);
    }


    @Override
    public void onError() {
        mPage--;
        mUnusecouponSmart.finishLoadmore(0);
        mUnusecouponMuti.setStatus(MutipleLayout.Error);
    }

    @Override
    public void onResultStart(HttpWrapper<List<CouponBean>> data) {
        mUnusecouponSmart.finishRefresh(0);
        if (Constant.SUCCESS_CODE.equals(data.code)) {
            if (data.data != null && data.data.size() > 0) {
                mCouponAdapter.addData(data.data);
                mUnusecouponMuti.setStatus(MutipleLayout.Success);
            } else {
                mUnusecouponMuti.setStatus(MutipleLayout.Empty);
            }
        } else if (Constant.ERROR_CODE.equals(data.getCode())) {
            showMessage(data.getInfo());
            onError();
        } else if (Constant.TOKEN_TIMEOUT.equals(data.code)) {
            U.tokenOut(mActivity);
        }
    }

    @Override
    public void onResultLoadMore(HttpWrapper<List<CouponBean>> o) {
        mUnusecouponSmart.finishLoadmore(0);
        if (Constant.SUCCESS_CODE.equals(o.getCode())) {
            if (Constant.SUCCESS_CODE.equals(o.getCode())) {
                if (o.data != null && o.data.size() > 0) {
                    mCouponAdapter.addData(o.data);
                } else {
                    showMessage("没有更多数据了");
                    mPage--;
                }
            }
        } else if (Constant.ERROR_CODE.equals(o.getCode())) {
            showMessage(o.getInfo());
            onError();
        } else if (Constant.TOKEN_TIMEOUT.equals(o.code)) {
            U.tokenOut(mActivity);
        }
    }

    @Override
    protected void initListener() {
        mUnusecouponSmart.setOnRefreshListener(refreshlayout -> {
            getData();
        });

        mUnusecouponSmart.setOnLoadmoreListener(refreshlayout -> {
            mPresenter.loadMore(getUserId(), getToken(), ++mPage, mPageSize,mType);
        });

    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_unusecoupon;
    }

}
